const https = require('https'); //内置库
const cheerio =require('cheerio');
// const { resolve } = require('path/posix');
const url = 'https://book.douban.com/subject/35291248/'

// https
//     .get('https://book.douban.com/subject/35291248/',function(res){
//         // console.log(res,'------');
//         // 页面有点大，一段段的返回数据包
//         let html = '';
//         res.on('data' , function(chunk){
//             // console.log(chunk);
//             html +=chunk;
//         });
//         res.on('end',function(){
//             console.log(html);
//         })
//     })
const httpPromise = new Promise((resolve,reject)=>{
    try{
        let html
        https
            .get(url,(res)=>{
                res.on('data',(chunk)=>{
                    html += chunk;
                })
                res.on('end',()=>{
                    resolve(html);
                })
            })
    }catch(e){
        reject(e);
    }
})

// httpPromise
//     .then(res =>{
//         console.log(res);
//     })
//     .catch(err =>{
//         console.log(err);
//     })
// 单点入口
const main = async () =>{
  let html = await httpPromise;
//   console.log(html);
  const $ = cheerio.load(html);
  const title = $('#wrapper h1 span').text();
  const author = $('#info span a').text();
  const pubCompany = $('.intro  p').text()
  console.log(title,author,pubCompany);
}
main();